<script setup lang="ts">
import { Form } from '@/components/Form'
import { reactive, ref, unref } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useForm } from '@/hooks/web/useForm'
import { ElButton, ElInput, FormRules, ElLink } from 'element-plus'
import { useValidator } from '@/hooks/web/useValidator'
import { FormSchema } from '@/types/form'
import { modifyPassword } from '@/api/login'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const emit = defineEmits(['to-login'])

const { register, elFormRef, methods } = useForm()

const { go } = useRouter()

const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'title',
    colProps: {
      span: 24
    }
  },
  {
    field: 'old_password',
    label: '原密码',
    value: '',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      style: {
        width: '100%'
      },
      strength: true,
      placeholder: '请输入原密码'
    }
  },
  {
    field: 'new_password',
    label: '新密码',
    value: '',
    component: 'InputPassword',
    colProps: {
      span: 24
    },
    componentProps: {
      style: {
        width: '100%'
      },
      strength: true,
      placeholder: '请输入新密码'
    }
  },
  {
    field: 'confirm_new_password',
    label: t('login.checkPassword'),
    value: '',
    component: 'InputPassword',
    colProps: {
      span: 24
    },
    componentProps: {
      style: {
        width: '100%'
      },
      strength: true,
      placeholder: '请输入确认密码'
    }
  },
  {
    field: 'register',
    colProps: {
      span: 24
    }
  }
])

const validatePass2 = (_rule: any, value: any, callback: any) => {
  const { getFormData } = methods
  getFormData().then((formData) => {
    if (value !== formData?.new_password) {
      callback(new Error('两次密码输入不一致'))
    } else {
      callback()
    }
  })
}

const rules: FormRules = {
  old_password: [required(t('common.required'))],
  new_password: [required(t('common.required'))],
  confirm_new_password: [
    required(t('common.required')),
    { validator: validatePass2, trigger: 'blur' }
  ]
}

const toLogin = () => {
  emit('to-login')
}

const loading = ref(false)

const formValue = ref<Recordable>({})

const doModifyPassword = async () => {
  const formRef = unref(elFormRef)
  formRef?.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const { getFormData } = methods
        const formData = await getFormData()
        console.log(formData)
        await modifyPassword(formData)
        ElMessage.success('修改成功')
        toLogin()
      } finally {
        loading.value = false
      }
    }
  })
}

const goBack = () => {
  go(-1)
}
</script>

<template>
  <Form
    :model="formValue"
    :schema="schema"
    :rules="rules"
    label-position="top"
    hide-required-asterisk
    size="large"
    class="dark:(border-1 border-[var(--el-border-color)] border-solid)"
    @register="register"
  >
    <template #title>
      <h2 class="text-2xl font-bold text-center w-[100%]">{{ t('login.modifypassword') }}</h2>
    </template>

    <template #code="form">
      <div class="w-[100%] flex">
        <ElInput v-model="form['code']" :placeholder="t('login.codePlaceholder')" />
      </div>
    </template>

    <template #register>
      <div class="w-[100%]">
        <ElButton type="primary" class="w-[100%]" :loading="loading" @click="doModifyPassword">
          {{ '修改' }}
        </ElButton>
      </div>
      <div class="w-[100%] text-right">
        <ElLink class="mt-4" type="primary" :loading="loading" @click="goBack">
          {{ t('common.back') }}
        </ElLink>
      </div>
    </template>
  </Form>
</template>
